/**
 *    Copyright 2006-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.plugins.custome;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Bean工具类.底层排除了Iteger为null的时候自动补充0的问题
 *
 * @author <a href="http://www.jiangzezhou.com">jiangzezhou</a>
 * @version 1.0.0.0, 2/27/16 12::12
 */
public class NewBstBeanUtils {

    private static final Logger LOGGER = Logger.getLogger(NewBstBeanUtils.class);

    public static <T> T po2dto(Object po, Class<T> dtoClass) {
        if (po == null) {
            return null;
        }
        try {
            T target = dtoClass.newInstance();
            BeanUtils.copyProperties(po, target);
            return target;
        } catch (Exception e) {
            LOGGER.error("po2dto  error", e);
            throw new ServiceRuntimeException(ExceptionCode.SYS_ERR, e);
        }
    }

    public static <T> T dto2po(Object dto, Class<T> poClass) {
        if (dto == null) {
            return null;
        }
        try {
            T target = poClass.newInstance();
            BeanUtils.copyProperties(dto, target);
            return target;
        } catch (Exception e) {
            throw new ServiceRuntimeException(ExceptionCode.SYS_ERR, e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> dtoList2poList(List<?> dtoList, Class<T> poClass) {
        if (dtoList == null || dtoList.size() < 1) {
            return null;
        }
        ArrayList newList = new ArrayList();
        try {
            for (Iterator i$ = dtoList.iterator(); i$.hasNext(); ) {
                Object t = i$.next();
                T target = poClass.newInstance();
                BeanUtils.copyProperties(t, target);
                newList.add(target);
            }
        } catch (Exception e) {
            throw new ServiceRuntimeException(ExceptionCode.SYS_ERR, e);
        }
        return newList;
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> poList2dtoList(List<?> poList, Class<T> dtoClass) {
        if (poList == null || poList.size() < 1) {
            return null;
        }
        ArrayList newList = new ArrayList();
        try {
            for (Iterator i$ = poList.iterator(); i$.hasNext(); ) {
                Object t = i$.next();
                T target = dtoClass.newInstance();
                BeanUtils.copyProperties(t, target);
                newList.add(target);
            }
        } catch (Exception e) {
            throw new ServiceRuntimeException(ExceptionCode.SYS_ERR, e);
        }
        return newList;
    }


    public static <T> T dto2Po(Object dto, Class<T> poClass) {
        try {
            T target = poClass.newInstance();
            BeanUtils.copyProperties(dto, target);
            return target;
        } catch (Exception e) {
            LOGGER.error("dto2Po  error", e);
            throw new ServiceRuntimeException(ExceptionCode.SYS_ERR, e);
        }
    }

    public static void copyProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    public static void copyPropertiesExcludeNull(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullProPertyNames(source));
    }

    public static String[] getNullProPertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
}
